package com.lovecaa;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

/**
 * 单向发送消息
 * 创建时间： 2023年10月20日 23:29:00
 *
 * @author Mr.Tao
 * @since 1.0.0
 */
public class OnewayProducer {
    public static void main(String[] args) {
        try {
            DefaultMQProducer producer = new DefaultMQProducer("OnewayProducer");
            producer.setNamesrvAddr("192.168.192.110:9876");
            producer.start();
            for (int i = 0; i < 2; i++) {
                String topic = "TopicTest";
                String tag = "TagA";
                byte[] msg = ("OnewayProducer_" + i).getBytes(StandardCharsets.UTF_8);
                Message message = new Message(topic, tag, msg);
                producer.sendOneway(message);
                System.out.println("消息" + i + "发送成功");
            }
            producer.shutdown();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}
